package com.byapp.fast.common.converter;

import com.byapp.fast.common.utils.JsonUtils;
import com.byapp.fast.modules.order.entity.OrderItemEntity;
import com.byapp.fast.modules.order.entity.OrderItemExtEntity;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class OrderItemExtConverter implements ConverterFactory<OrderItemExtEntity, OrderItemEntity> {

    private static Map<String, Object> map = new HashMap<>();


    @Override
    public OrderItemExtEntity create(OrderItemEntity orderItemEntity) {
        OrderItemExtEntity orderItemExtEntity = new OrderItemExtEntity();
        Map<String, Object> matchInfo = new HashMap<>();
        map.put(orderItemEntity.getBalanceMatchId(), matchInfo);
        matchInfo.put("HOME_TEAM", orderItemEntity.getHomeTeamName());
        matchInfo.put("AWAY_TEAM", orderItemEntity.getAwayTeamName());
        matchInfo.put("MATCH_TIME", orderItemEntity.getMatchTime());
        matchInfo.put("LEAGUE_NAME", orderItemEntity.getLeagueName());
        orderItemExtEntity.setItemId(orderItemEntity.getItemId());
        orderItemExtEntity.setMatchInfo(JsonUtils.toNormalJson(map));
        orderItemExtEntity.setUserId(orderItemEntity.getUserId());
        return orderItemExtEntity;
    }

    public static List<OrderItemExtEntity> mapToTarget(List<OrderItemEntity> list) {
        return !CollectionUtils.isEmpty(list) ? list.stream()
                .map(new OrderItemExtConverter()::create)
                .collect(Collectors.toList()) : null;
    }

    public static OrderItemExtEntity mapToTarget(OrderItemEntity orderItemEntity) {
        return new OrderItemExtConverter().create(orderItemEntity);
    }

}
